package com.gp.controller.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.gp.common.result.JsonResult;
import com.gp.common.utils.CookieUtils;
import com.gp.common.utils.StringUtil;
import com.gp.controller.BaseController;
import com.gp.model.entity.BaseCouponDistribution;
import com.gp.model.entity.BaseOrderCoupon;
import com.gp.model.entity.BaseUser;
import com.gp.service.IBaseCouponDistributionService;
import com.gp.service.IBaseOrderCouponService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

/**
 * @author: 弓成龙
 * @date : 2018年3月1日
 * @email : gcx909109@sina.cn
 * @description: 优惠券
 */
@RestController
@Api(description = "优惠券", value = "优惠券")
@RequestMapping(value = "/coupon")
public class CouponController extends BaseController{

  @Autowired
  private IBaseCouponDistributionService couponlogService;

  @Autowired
  private IBaseOrderCouponService couponService;

  @RequestMapping(value = "/add", method = RequestMethod.POST)
  @ApiOperation(value = "创建优惠券记录")
  public JsonResult add(HttpServletRequest request, HttpServletResponse response,
      @Validated BaseCouponDistribution coupon, BindingResult bindingResult) {
    boolean result = couponlogService.insert(coupon);
    return result ? new JsonResult("创建成功", result) : new JsonResult("创建失败", result);
  }

  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ApiOperation(value = "获取当前用户所有的优惠券", httpMethod = "GET")
  public JsonResult list(HttpServletRequest request) {
    BaseUser user = (BaseUser) CookieUtils.obtainCurrentUser(request);
    if (user == null) {
      return new JsonResult("500", "获取失败");
    }
   List<BaseCouponDistribution> list = couponlogService.selectCouponListByUserId(user.getId());
    return new JsonResult("获取成功", list);
  }
  
  @RequestMapping(value = "/list/no", method = RequestMethod.GET)
  @ApiOperation(value = "获取当前用户所有的未使用的优惠券", httpMethod = "GET")
  public JsonResult listNo(HttpServletRequest request) {
    Map<String, Object> columnMap =  new HashMap<String, Object>();
    BaseUser user = (BaseUser) CookieUtils.obtainCurrentUser(request);
    if (user == null) {
      return new JsonResult("500", "获取失败");
    }
    columnMap.put("userId", user.getId());
    columnMap.put("isValid", 0);
    List<BaseCouponDistribution> list = couponlogService.selectByMap(columnMap);
    return new JsonResult("获取成功", list);
  }
  
  @RequestMapping(value = "/list/yes", method = RequestMethod.GET)
  @ApiOperation(value = "获取当前用户所有的使用的优惠券", httpMethod = "GET")
  public JsonResult listYes(HttpServletRequest request) {
    Map<String, Object> columnMap =  new HashMap<String, Object>();
    BaseUser user = (BaseUser) CookieUtils.obtainCurrentUser(request);
    if (user == null) {
      return new JsonResult("500", "获取失败");
    }
    columnMap.put("userId", user.getId());
    columnMap.put("isValid", 1);
    List<BaseCouponDistribution> list = couponlogService.selectByMap(columnMap);
    return new JsonResult("获取成功", list);
  }

  @RequestMapping(value = "/del/{couponId}", method = RequestMethod.POST)
  @ApiOperation(value = "删除优惠券记录")
  public JsonResult del(HttpServletRequest request, @PathVariable Long couponId) {
    boolean result = couponlogService.deleteById(couponId);
    return result ? new JsonResult("删除成功", result) : new JsonResult("删除失败", result);
  }

  /**
   * 管理员创建优惠券
   */
  @RequestMapping(value = "/create", method = RequestMethod.POST)
  @ApiOperation(value = "创建优惠券")
  public JsonResult create(BaseOrderCoupon coupon) {
    boolean result = couponService.insert(coupon);
    return result ? new JsonResult("创建成功", result) : new JsonResult("创建失败", result);
  }

  @RequestMapping(value = "/dsitribution", method = RequestMethod.POST)
  @ApiOperation(value = "向用户发送优惠券")
  public JsonResult distributeCoupon(Long couponId, String userIds) {
    List<Long> ids = StringUtil.StrToLongList(userIds);
    boolean result = couponService.distributeCoupon(couponId, ids);
    return result ? new JsonResult("创建成功", result) : new JsonResult("创建失败", result);
  }

  @RequestMapping(value = "/detail/{couponId}", method = RequestMethod.POST)
  @ApiOperation(value = "查询一个优惠券的详细信息")
  public JsonResult detail(@PathVariable Long couponId) {
    BaseOrderCoupon result = couponService.selectById(couponId);
    return new JsonResult("查询成功", result);
  }
}

